<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>祖辈与孙辈关系 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/grandparents.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/grandparents.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/grandparents.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/grandparents.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="modeling-your-data.html">数据建模</a></span>
»
<span class="breadcrumb-link"><a href="parent-child.html">父-子关系文档</a></span>
»
<span class="breadcrumb-node">祖辈与孙辈关系</span>
</div>
<div class="navheader">
<span class="prev">
<a href="children-agg.html">« 子文档聚合</a>
</span>
<span class="next">
<a href="parent-child-performance.html">实际使用中的一些建议 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="grandparents"></a>祖辈与孙辈关系 (Grandparents and Grandchildren)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/404_Parent_Child/65_Grandparents.asciidoc">edit</a>
</h2>
</div></div></div>
<p>父子关系可以延展到更多代，比如生活中孙辈与祖辈的关系 — 唯一的要求是满足这些关系的文档必须索引在同一个分片上。</p>

<p>让我们把上一个例子中的<code class="word">country</code>类型设定为<code class="word">branch</code>类型的父辈：</p>

<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /company
{
  "mappings": {
    "country": {},
    "branch": {
      "_parent": {
        "type": "country" <a id="CO283-1"></a><i class="conum" data-value="1"></i>
      }
    },
    "employee": {
      "_parent": {
        "type": "branch" <a id="CO283-2"></a><i class="conum" data-value="2"></i>
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO283-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="word">branch</code>是<code class="word">country</code>的子辈。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO283-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="word">employee</code>是<code class="word">branch</code>的子辈。</p>
</td>
</tr>
</table>
</div>
<p>country 和 branch 之间是一层简单的父子关系，所以我们的操作步骤与<a class="xref" href="indexing-parent-child.html" title="构建父-子文档索引">构建父-子文档索引</a>保持一致：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">POST /company/country/_bulk
{ "index": { "_id": "uk" }}
{ "name": "UK" }
{ "index": { "_id": "france" }}
{ "name": "France" }

POST /company/branch/_bulk
{ "index": { "_id": "london", "parent": "uk" }}
{ "name": "London Westmintster" }
{ "index": { "_id": "liverpool", "parent": "uk" }}
{ "name": "Liverpool Central" }
{ "index": { "_id": "paris", "parent": "france" }}
{ "name": "Champs Élysées" }</pre>
</div>
<p><code class="literal">parent</code> ID 使得每一个<code class="word">branch</code>文档被路由到与其父文档<code class="word">country</code>相同的分片上进行操作。然而，当我们使用相同的方法来操作<code class="word">employee</code>这个孙辈文档时，会发生什么呢？</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /company/employee/1?parent=london
{
  "name":  "Alice Smith",
  "dob":   "1970-10-24",
  "hobby": "hiking"
}</pre>
</div>
<p><code class="word">employee</code>文档的路由依赖其父文档 ID — 也就是<code class="word">london</code> — 但是<code class="word">london</code>文档的路由却依赖 <em>其本身的</em> 父文档 ID — 也就是<code class="word">uk</code>。此种情况下，孙辈文档很有可能最终和父辈、祖辈文档不在同一个分片上，导致不满足祖辈和孙辈文档必须索引在同一个分片上的要求。</p>

<p>解决方案是添加一个额外的参数<code class="literal">routing</code>，将其设置为祖辈的文档 ID ，以此来保证三代文档路由到同一个分片上。索引请求如下所示：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /company/employee/1?parent=london&amp;routing=uk <a id="CO284-1"></a><i class="conum" data-value="1"></i>
{
  "name":  "Alice Smith",
  "dob":   "1970-10-24",
  "hobby": "hiking"
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO284-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">routing</code>的值会取代<code class="literal">parent</code>的值作为路由选择。</p>
</td>
</tr>
</table>
</div>
<p>参数<code class="literal">parent</code>的值仍然可以标识 employee 文档与其父文档的关系，但是参数<code class="literal">routing</code>保证该文档被存储到其父辈和祖辈的分片上。<code class="literal">routing</code>值在所有的单个文档请求中都要添加。</p>

<p>可以跨多代文档进行查询和聚合，只需要一代代的进行设定即可。例如，我们要找到哪些国家的员工喜欢徒步旅行，此时只需要关联 country 和 branch，以及 branch 和 employee：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /company/country/_search
{
  "query": {
    "has_child": {
      "type": "branch",
      "query": {
        "has_child": {
          "type": "employee",
          "query": {
            "match": {
              "hobby": "hiking"
            }
          }
        }
      }
    }
  }
}</pre>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="children-agg.html">« 子文档聚合</a>
</span>
<span class="next">
<a href="parent-child-performance.html">实际使用中的一些建议 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>